<template>
  <div class="sso"></div>
</template>
<script>
export default {
  mounted() {
    const authToken = this.$route.query.token;
    // console.log('🚀 ~ file: sso.vue ~ line 8 ~ mounted ~ authToken', authToken)
    const { logout } = this.$route.query;
    const { redirect } = this.$route.query;
    // console.log('🚀 ~ file: sso.vue ~ line 9 ~ mounted ~ authToken', authToken)
    // https://zhangyu.zwwlkj03.top/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxODIzNjUzNjE2OCIsImxvZ2luX2VudiI6IiIsImlhdCI6MTY4MTM3ODQ1MCwiZXhwIjoxNjgxOTgzMjUwLCJuYmYiOjE2ODEzNzg0NTAsInN1YiI6InRva2Vu6K6k6K-BIiwianRpIjoiZGNmMTk1Njg2YzY0OGUwNTFmZTg4OTZmMjU1MzcyNDgifQ.EKJWWAEcqVZayFfq703h5u6TS1oLXJfkUkHnyb8ONxs&redirect=#/sso?redirect=%2Fdashboard
    // https://companywx.zwwlkj03.top/sso?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxODIzNjUzNjE2OCIsImxvZ2luX2VudiI6IiIsImlhdCI6MTY4MjA0NzgwNiwiZXhwIjoxNjgyNjUyNjA2LCJuYmYiOjE2ODIwNDc4MDYsInN1YiI6InRva2Vu6K6k6K-BIiwianRpIjoiZTk3NzZlOTc2YjMzNTk1MzFiYWM4YWQ3NmIxZDJiODIifQ.jVVrT1826VC0Q7XpC2khoMVyBdzb80DhsPKW_80xh48&
    if (logout) {
      window.location.href = import.meta.env.VITE_APP_ZQLOGIN_URL;
    } else if (authToken) {
      this.$store
        .dispatch('user/ZqLogin', authToken)
        .then((res) => {
          console.log('新的token:', authToken);
          this.$message.success('登录成功');
          this.$router.push({ path: redirect || '/' });
        })
        .catch((err) => {
          console.log(err);
        });
    } else {
      const api_url = () => {
        if (import.meta.env.MODE === 'development') {
          return 'https://zhangyu.zwwlkj03.top/api/admin/public/sso';
        }
        return `${window.location.origin + import.meta.env.VITE_APP_BASE_API}/admin/public/sso`;
        // console.log(process)
      };
      const redirect_url = () => {
        if (import.meta.env.MODE === 'development') {
          return encodeURIComponent(`${window.location.origin}/#/sso`);
        }
        return encodeURIComponent(`${window.location.origin}/sso`);
      };
      const url = `${import.meta.env.VITE_APP_ZQLOGIN_URL}/login?api_url=${api_url()}&redirect_url=${redirect_url()}`;
      window.location.href = url;
    }
  },
};
</script>
